import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'
import mockjs from 'mockjs'
import type { Plugin } from 'vite'
import tailwindcss from '@tailwindcss/vite'
import url from 'node:url'


// 1. vite插件
const viteMockServer = ():Plugin =>{
  return {
    name:"vite-mock-server",
    // configureServer hook 返回当前的服务
    configureServer(server){
     server.middlewares.use("/api/list", (req, res)=>{ 
      const parseUrl = url.parse(req.originalUrl!,true).query
      res.setHeader('content-type','application/json')
      const data =mockjs.mock({
        'list|1000':[
          {
            'id|+1':1,
            name:parseUrl.keyWord,
            'address|1':'@county(true)',
          }
        ]
      })
      res.end(JSON.stringify(data))
     })
    }
  }
}


// https://vite.dev/config/
export default defineConfig({
  css:{
    modules:{
      // camelCase  camelCaseOnly   dashes  dashesOnly
      localsConvention:'camelCase', // 修改css modules的类名规则 可以改成驼峰命名 或 xxx-xxx命名等
      generateScopedName: '[name]__[local]__[hash:base64:5]' // 修改css modules的类名规则(最终要生成的) name：文件名称  local：类名 hash:base64:5：hash随机数的base64编码5位
    }
  },
  plugins: [react(), viteMockServer(),tailwindcss()],
})
